import { combineReducers } from 'redux'
import {reducer as user} from './user'
import {reducer as project} from './project'
import {reducer as category} from './category'
import {reducer as schedule} from './schedule'
import {reducer as milestone} from './milestone'
import {reducer as evaluate} from './evaluate'
import {reducer as search} from './search'
import {reducer as assign} from './assign'


const initialState = {
    isFetching: true,
    msg: {
        type: 'success',//0失败 1成功
        content: ''
    },
    token: '',
    shouldRender: false,
    isWechat: false,
    entry: '/',
    openid: '',
    shouldRefresh: false
};

export const actionsTypes = {
    FETCH_START: "FETCH_START",
    FETCH_END: "FETCH_END",
    SET_MESSAGE: "SET_MESSAGE",
    USER_LOGIN: "USER_LOGIN",
    USER_REGISTER: "USER_REGISTER",
    USER_AUTH:"USER_AUTH",
    RESPONSE_TOKEN:"RESPONSE_TOKEN",
    USER_LOGOUT:"USER_LOGOUT",
    RESOLVE_ISWECHAT: " RESOLVE_ISWECHAT",
    CHANGE_SHOULDRENDER: "CHANGE_SHOULDRENDER",
    CHANGE_ENTRY: "CHANGE_ENTRY",
    UPDATE_OPENID: "UPDATE_OPENID",
    CHANGE_SHOULDREFRESH: "CHANGE_SHOULDREFRESH"
};

export const actions = {
    get_login: function (data) {
        return {
            type: actionsTypes.USER_LOGIN,
            data
        }
    },
    get_register: function (data) {
        return {
            type: actionsTypes.USER_REGISTER,
            data
        }
    },
    get_logout: function () {
        return {
            type: actionsTypes.USER_LOGOUT
        }
    },
    clear_msg: function () {
        return {
            type: actionsTypes.SET_MESSAGE,
            msgType: 1,
            msgContent: ''
        }
    },
    user_auth: function () {
        return {
            type: actionsTypes.USER_AUTH,
        }
    },
    change_entry: function (entry) {
        return {
            type: actionsTypes.CHANGE_ENTRY,
            entry
        }
    },
    update_openid: function (openid) {
        return {
            type: actionsTypes.UPDATE_OPENID,
            openid
        }
    }
};

export function reducer(state = initialState, action) {
    switch (action.type) {
        case actionsTypes.FETCH_START:
            return {
                ...state, isFetching: true
            };
        case actionsTypes.FETCH_END:
            return {
                ...state, isFetching: false
            };
        case actionsTypes.SET_MESSAGE:
            return {
                ...state,
                isFetching: false,
                msg: {
                    type: action.msgType,
                    content: action.msgContent
                }
            };
        case actionsTypes.RESPONSE_TOKEN:
            localStorage.setItem('auth_token', action.data.access_token)
            return {
                ...state, token: action.data.access_token
            }   
        case actionsTypes.RESOLVE_ISWECHAT:
            return {
                ...state, isWechat: action.isWechat
            }    
        case actionsTypes.CHANGE_SHOULDRENDER:
            return {
                ...state, shouldRender: action.shouldRender
            }  
        case actionsTypes.CHANGE_ENTRY:
            return {
                ...state, entry: action.entry
            }  
        case actionsTypes.UPDATE_OPENID:
            return {
                ...state, openid: action.openid
            }   
        case actionsTypes.CHANGE_SHOULDREFRESH:
            return {
                ...state, shouldRefresh: action.shouldRefresh
            }                   
        default:
            return state
    }
}


export default combineReducers({
    globalState: reducer,
    user,
    project,
    category,
    schedule,
    milestone,
    evaluate,
    search,
    assign
})